package edu.lingnan.librarymanage.config.web;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MyWebMvcConfig implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        /**
         * 因为和普通用户集成在了一起，
         * 那么，如果是普通用户，而且又是必须要登录的请求怎么办?
         * 直接抛异常，提示未登录
         */
        MyHandlerInterceptor handlerInterceptor = new MyHandlerInterceptor();
        registry.addInterceptor(handlerInterceptor)
                .addPathPatterns("/**")
                // 放行首页
                .excludePathPatterns("/", "/index")
                // 静态资源
                .excludePathPatterns("/static/**")
                // 公共请求
                .excludePathPatterns("/common/**")
                // 注册请求
                .excludePathPatterns("/register")
                .excludePathPatterns("/borrow/**")
                .excludePathPatterns("/user/history")
                // 登录请求
                .excludePathPatterns("/login")
                // 退出请求
                .excludePathPatterns("/logout")
                // 注册页面
                .excludePathPatterns("/register.html")
                // 新增图书
                .excludePathPatterns("/addBook.html")
                // 管理员登录页面
                .excludePathPatterns("/login.html");
    }

    /**
     * 设置初始化请求
     *
     * @param registry
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/register.html").setViewName("/register");
        registry.addViewController("/admin").setViewName("/admin/index");
        registry.addViewController("/home.html").setViewName("/admin/home");
        registry.addViewController("/login.html").setViewName("/admin/login");
        registry.addViewController("/books.html").setViewName("/admin/books");
        registry.addViewController("/user.html").setViewName("/admin/user");
        registry.addViewController("/borrow.html").setViewName("/admin/borrow");
        registry.addViewController("/category.html").setViewName("/admin/category");
        registry.addViewController("/addBook.html").setViewName("/admin/addBook");
    }
}
